<?php
/**
 * 权限model
 */
class Npermissions_model extends CT_Model
{
	public $_table = 'nursery_action';
	
	public function __construct()
	{
		parent::__construct();
		$this->load->database();
	}


	/**
	 * 已知父级id获取该父级下面所有的权限集合
	 * @param int $pid
	 * @return mixed
	 */
	public function  get_all_permissions($pid=0)
	{
		$this->db->select('id,name');
		$this->db->where("pid",$pid);
		$res=$this->db->get($this->_table)->result_array();
		$return=array();
		foreach($res as $val){
			$id=$val["id"];
			$return[$id]=$val["name"];
		}
		return $return;
	}


	//判断权限名称是否重复
	public  function  check_permissions_name($permissions_name,$permissions_id=NULL)
	{

		if(empty($permissions_id)){
			$this->db->where('name',$permissions_name);
		}else{
			$this->db->where('id <>',$permissions_id);
			$this->db->where('name',$permissions_name);
		}

		$data = $this->db->select('id')->get($this->_table)->row_array();
		return $data['id'];

	}

	//判断权限链接是否重复
	public  function check_link($link,$id=NULL)
	{
		if(empty($id)){
			$this->db->where('link',$link);
		}else{
			$this->db->where('id <>',$id);
			$this->db->where('link',$link);
		}

		$data = $this->db->select('id')->get($this->_table)->row_array();
		return $data['id'];
	}
	
}